package com.osmino.contacts.dialer;

import java.util.List;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import com.osmino.contacts.PreferenceKeys;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Call;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.entity.Phone;
import com.osmino.contacts.help.HelpActivity;
import com.osmino.contacts.provider.CallLogQueries;
import com.osmino.contacts.provider.ContactsContractResolver;
import com.osmino.contacts.provider.ContactsContractResolver.ContactsDataObserver;
import com.osmino.contacts.settings.SettingsActivity;
import com.osmino.core.AbsActivity;
import com.osmino.core.DialogGenerator;
import com.osmino.core.style.concrete.Default;

public final class DialerActivity extends AbsActivity implements IDialerController, ContactsDataObserver {

	boolean DBG = true;
	private ContactsContractResolver mContactsContractResolver;
	private DialogGenerator mDialogGenerator;
	private DialerView mDialerView;
	private Vibrator mVibrator;
	private boolean mDialerShown;
	private boolean mShouldPlayButton;
	private boolean mShouldVibrateButton;
	private DialerAdapter mAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContactsContractResolver = mHostApplication.getContactsContractResolver();
		mDialogGenerator = new DialogGenerator(this, this);
		mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		String prefKey = PreferenceKeys.PREF_BUTTON_VIBRATION;
		mShouldVibrateButton = mApplicationPreferences.getBoolean(prefKey, true);
		prefKey = PreferenceKeys.PREF_BUTTON_SOUNDS;
		mShouldPlayButton = mApplicationPreferences.getBoolean(prefKey, true);
		mDialerShown = true;
		mDialerView = new DialerView(this, this, new Default());
		setContentView(mDialerView);
		Cursor callCursor = CallLogQueries.getAllCalls(this);
		startManagingCursor(callCursor);
		mAdapter = new DialerAdapter(this, callCursor);
		mDialerView.getListView().setAdapter(mAdapter);
		mContactsContractResolver.registerContactsDataObserver(this);
		findViewById(R.id.sideIndex).setVisibility(View.GONE);
		if (!mContactsContractResolver.isContactListsReady()) {
			showDialog(R.id.dialog_waiting);
		} else {
			List<Contact> listContacts = mContactsContractResolver.getListContacts();
			mAdapter.setListContacts(listContacts);			
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mHostApplication.getDialerStateModel().registerClient(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mHostApplication.getDialerStateModel().unregisterClient();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mContactsContractResolver.unregisterContactsDataObserver(this);
	}

	@Override
	public void onCallTap(String text) {
		if (text == null || text.length() < 1)
			return;
		Uri callUri = Uri.parse("tel:" + text);
		Intent callIntent = new Intent(Intent.ACTION_CALL, callUri);
		startActivity(callIntent);
	}

	@Override
	public int getListRegime() {
		return LIST_REGIME_OVERVIEW;
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		int viewId = adapterView.getId();
		if (viewId == android.R.id.list) {
			int adapterRegime = mAdapter.getRegime();
			if (adapterRegime == DialerAdapter.REGIME_CALLS) {
				Call call = (Call) mAdapter.getItem(position);
				onCallTap(call.number);
			} else if (adapterRegime == DialerAdapter.REGIME_CONTACTS) {
				Contact contact = (Contact) mAdapter.getItem(position);
				Phone phone = contact.getPhones().get(0);
				onCallTap(phone.number);
			}
		}
	}

	@Override
	public void onStateToggled() {
		mDialerShown = !mDialerShown;
		mDialerView.switchBetweenDialerAndList();
	}

	@Override
	public void onTextChanged(String text) {
		mAdapter.getFilter().filter(text);
	}
	
	@Override
	public void onDialerButtonClick() {
		if (mShouldVibrateButton) {
			mVibrator.vibrate(1000);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;
		switch (id) {
			case R.id.dialog_quick_message:
				dialog = mDialogGenerator.createQuickMessageDialog(null, null);
				break;
			case R.id.dialog_waiting:
				dialog = mDialogGenerator.createWaitingDialog();
				break;
			default:
				dialog = super.onCreateDialog(id);
		}
		return dialog;
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
			case R.id.dialog_quick_message:
				mDialogGenerator.prepareQuickMessageDialog((AlertDialog) dialog);
				break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.dialer_option_menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int itemId = item.getItemId();
		switch (itemId) {
			case R.id.dialer_option_call:
				onCallTap(mDialerView.getText());
				break;
			case R.id.dialer_option_btn_sounds:
				onButtonSoundsOption();
				break;
			case R.id.dialer_option_btn_vibration:
				onButtonVibrationOption();
				break;
			case R.id.dialer_option_connect_vibration:
				onConnectVibrationOption();
				break;
			case R.id.dialer_option_after_call_vibration:
				onAfterCallVibrationOption();
				break;
			case R.id.dialer_option_settings:
				onSettingsOption();
				break;
			case R.id.dialer_option_help:
				onHelpOption();
				break;
		}
		return false;
	}

	private void onButtonSoundsOption() {
		String prefKey = PreferenceKeys.PREF_BUTTON_SOUNDS;
		mShouldPlayButton = !mShouldPlayButton;
		mApplicationPreferencesEditor.putBoolean(prefKey, mShouldPlayButton).commit();
	}

	private void onButtonVibrationOption() {
		String prefKey = PreferenceKeys.PREF_BUTTON_VIBRATION;
		mShouldVibrateButton = !mShouldVibrateButton;
		mApplicationPreferencesEditor.putBoolean(prefKey, mShouldVibrateButton).commit();
	}

	private void onConnectVibrationOption() {
		String prefKey = PreferenceKeys.PREF_CONNECT_VIBRATION;
		boolean vibrate = mApplicationPreferences.getBoolean(prefKey, false);
		mApplicationPreferencesEditor.putBoolean(prefKey, !vibrate).commit();
	}

	private void onAfterCallVibrationOption() {
		String prefKey = PreferenceKeys.PREF_END_CALL_VIBRATION;
		boolean vibrate = mApplicationPreferences.getBoolean(prefKey, false);
		mApplicationPreferencesEditor.putBoolean(prefKey, !vibrate).commit();
	}

	private void onSettingsOption() {
		Intent settingsCall = new Intent(this, SettingsActivity.class);
		startActivity(settingsCall);
	}

	private void onHelpOption() {
		Intent helpCall = new Intent(this, HelpActivity.class);
		startActivity(helpCall);
	}

	@Override
	public void onStartRefresh() {
		showDialog(R.id.dialog_waiting);
	}
	
	@Override
	public void onRefreshContactsSctructure() {
		dismissDialog(R.id.dialog_waiting);
		List<Contact> listContacts = mContactsContractResolver.getListContacts();
		mAdapter.setListContacts(listContacts);
	}
}
